package com.jt.www.common.service.impl;

import com.jt.www.dao.mapper.messageinfo.MessageInfoEntityMapper;
import com.jt.www.dao.mapper.rabbitmqrecord.RabbitmqRecordEntityMapper;
import com.jt.www.model.entity.MessageInfoEntity;
import com.jt.www.model.entity.RabbitmqRecordEntity;
import com.jt.www.model.vo.RabbitMessageInfoVo;
import com.jt.www.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author admin_ltf
 * @create 2019-04-16 11:13
 */
@Service
public class MessageQueueListnerServiceImpl {

    @Autowired
    RabbitmqRecordEntityMapper rabbitmqRecordEntityMapper;
    @Autowired
    MessageInfoEntityMapper messageInfoEntityMapper;

    @Transactional
    public void insertMesageInfo(RabbitMessageInfoVo rabbitMessageInfoVo, Long id, String messageId) {

        // 消息消费 入库 message_info
        MessageInfoEntity messageInfoEntity = new MessageInfoEntity();
        String currentTime = DateUtils.getCurrentTime();
        messageInfoEntity.setRecordType(1);//消息记录类型: 1-个人消息
        messageInfoEntity.setTargetUser(rabbitMessageInfoVo.getTargetUser());//目标用户
        messageInfoEntity.setTargetOrganization(rabbitMessageInfoVo.getTargetOrganization());//目标机构
        messageInfoEntity.setTargetArea(rabbitMessageInfoVo.getTargetArea());//目标地区
        messageInfoEntity.setMessageType(rabbitMessageInfoVo.getMessageType());//消息类型
        messageInfoEntity.setMessageTitle(rabbitMessageInfoVo.getMessageTitle());//消息标题
        messageInfoEntity.setMessageContent(rabbitMessageInfoVo.getMessageContent());//消息内容 ???
        messageInfoEntity.setMessageUrl(rabbitMessageInfoVo.getMessageUrl());//消息链接
        messageInfoEntity.setNoticeType(rabbitMessageInfoVo.getNoticeType());//通知方式:0-无需通知；1-邮件推送；2-手机短信通知
        messageInfoEntity.setNoticeState(false);//通知状态:0-未通知；1-已通知
        messageInfoEntity.setComment(rabbitMessageInfoVo.getComment());//备注  备注写什么？
        messageInfoEntity.setIsDeleted(false);// 是否删除:1-是；0-否
        messageInfoEntity.setCreateTime(currentTime);//创建时间
        messageInfoEntity.setUpdateTime(currentTime);// 修改时间
        int msgnum = messageInfoEntityMapper.insert(messageInfoEntity);
        // 消息消费完之后， 修改rabbit_record  记录的状态  根据uuid做条件
//            rabbitmqRecordEntityMapper.updateRabbitrecordStateByuuid(messageId,true);
        // 改为不用自己手写sql的
        RabbitmqRecordEntity rabbitmqRecordEntity = new RabbitmqRecordEntity();
        rabbitmqRecordEntity.setId(id);
        rabbitmqRecordEntity.setIsSend(true);
        rabbitmqRecordEntity.setUpdateTime(new Date());
        rabbitmqRecordEntityMapper.updateByPrimaryKeySelective(rabbitmqRecordEntity);

    }
}
